package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员服务
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public Member findMemberByTelophone(String phoneNumber) {
        return memberDao.findMemberByTelephone(phoneNumber);
    }

    @Override
    public void updateById(Member member) {
        memberDao.updateById(member);
    }

    @Override
    public String findPassword(String phoneNumber) {
        return memberDao.findPassword(phoneNumber);
    }

    @Override
    public void updatePassword(String newpassword, String phoneNumber2) {
        memberDao.updatePassword(newpassword, phoneNumber2);
    }

    @Override
    public Integer findIdByTelephone(String telephone) {
        return memberDao.findIdByTelephone(telephone);
    }

    @Override
    public void updatephoneNumber(Integer id, String telephone) {
        memberDao.updatephoneNumber(id, telephone);
    }

    @Override
    public Integer findUserIfExist(String telephone, String password) {
        return memberDao.findUserIfExist(telephone, password);
    }

    //查找健康管理师
    public List<Map<String, Object>> findManagerByRoleId() {
        List<String> nameList = memberDao.findManagerByRoleId();
        List<Map<String, Object>> list = new ArrayList();
        for (String s : nameList) {
            Map<String, Object> map = new HashMap();
            map.put("value", s);
            list.add(map);
        }
        return list;
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);

        Page<Member> members = memberDao.queryPage(queryString);
        for (Member member : members) {
            Date birthday = member.getBirthday();
            if (birthday != null) {
                Date now = new Date();
                long time = now.getTime() - birthday.getTime();
                long ageLong = time / (365 * 24 * 60 * 60 * 1000l);
                int age = (int) ageLong;
                member.setAge(age);
            }
        }
        return new PageResult(members.getTotal(), members.getResult());
    }

    //保存会员信息
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        member.setRegTime(new Date());
        memberDao.add(member);
    }

    //根据id删除会员
    public void deleteById(Integer id) {
        if (memberDao.findRelevance(id) <= 0) {
            memberDao.deleteById(id);
        }
    }

    //通过id查询会员
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //编辑会员
    public void edit(Member member) {
        Date birthday = member.getBirthday();
        if (birthday != null) {
            Date now = new Date();
            long time = now.getTime() - birthday.getTime();
            long ageLong = time / (365 * 24 * 60 * 60 * 1000l);
            int age = (int) ageLong;
            member.setAge(age);
        }
        memberDao.edit(member);
    }

    //导入excel
    public List<String> findNames(Integer id) {
        List<Map<String, Object>> list = memberDao.findNames(id);//所有的检查项信息 （套餐 检查组 检查项）
        HashSet<String> setmealSet = new HashSet();
        HashSet<String> checkgrouplSet = new HashSet();
        HashSet<String> checkitemsSet = new HashSet();
        //遍历数据 并获取对应数据
        for (Map<String, Object> map : list) {
            String setmeal = (String) map.get("setmeal");
            String checkgroup = (String) map.get("checkgroups");
            String checkitems = (String) map.get("checkitems");
            setmealSet.add(setmeal);
            checkgrouplSet.add(checkgroup);
            checkitemsSet.add(checkitems);
        }
        String setmealSetStr = setmealSet.toString();
        String checkgrouplSetStr = checkgrouplSet.toString();
        String checkitemsSetStr = checkitemsSet.toString();
        //添加数据
        List<String> nameList = new ArrayList();
        nameList.add(setmealSetStr);
        nameList.add(checkgrouplSetStr);
        nameList.add(checkitemsSetStr);
        return nameList;

    }

    @Override
    public List<Member> findAll() {
        return memberDao.queryAll();
    }

}
